<template>
  <div class="test-case-editor-container bg-dark">
    <!-- 顶部导航 -->
    <div class="bg-dark-header p-4 border-b border-gray-700 flex items-center justify-between">
      <div class="flex items-center space-x-4">
        <el-button type="text" icon="ArrowLeft" class="text-gray-300"></el-button>
        <h1 class="text-xl font-semibold text-white">测试用例编辑器</h1>
      </div>
      <div class="flex space-x-3">
        <el-button type="default" class="bg-gray-700 hover:bg-gray-600 text-white">保存草稿</el-button>
        <el-button type="primary" class="bg-primary hover:bg-primary/90 text-white">保存</el-button>
        <el-button type="success" class="bg-success hover:bg-success/90 text-white">运行</el-button>
      </div>
    </div>

    <!-- 主内容区 -->
    <div class="flex h-[calc(100vh-64px)]">
      <!-- 左侧属性面板 -->
      <div class="w-72 border-r border-gray-700 bg-dark-card p-4 overflow-y-auto">
        <h2 class="text-lg font-medium text-white mb-4">用例属性</h2>

        <div class="space-y-4">
          <div>
            <label class="block text-sm font-medium text-gray-400 mb-1">用例名称</label>
            <el-input v-model="caseName" placeholder="输入用例名称" class="bg-dark border-gray-700 text-white"></el-input>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-400 mb-1">所属项目</label>
            <el-select v-model="projectId" placeholder="选择项目" class="bg-dark border-gray-700 text-white">
              <el-option v-for="project in projects" :key="project.id" :label="project.name" :value="project.id"></el-option>
            </el-select>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-400 mb-1">所属测试集</label>
            <el-select v-model="suiteId" placeholder="选择测试集" class="bg-dark border-gray-700 text-white">
              <el-option v-for="suite in suites" :key="suite.id" :label="suite.name" :value="suite.id"></el-option>
            </el-select>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-400 mb-1">优先级</label>
            <el-radio-group v-model="priority" class="text-gray-300">
              <el-radio label="P0" border class="bg-dark-card text-gray-300">P0 (最高)</el-radio>
              <el-radio label="P1" border class="bg-dark-card text-gray-300">P1 (高)</el-radio>
              <el-radio label="P2" border class="bg-dark-card text-gray-300">P2 (中)</el-radio>
              <el-radio label="P3" border class="bg-dark-card text-gray-300">P3 (低)</el-radio>
            </el-radio-group>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-400 mb-1">用例类型</label>
            <el-select v-model="caseType" placeholder="选择类型" class="bg-dark border-gray-700 text-white">
              <el-option label="功能测试" value="functional"></el-option>
              <el-option label="UI测试" value="ui"></el-option>
              <el-option label="API测试" value="api"></el-option>
              <el-option label="性能测试" value="performance"></el-option>
            </el-select>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-400 mb-1">状态</label>
            <el-select v-model="status" placeholder="选择状态" class="bg-dark border-gray-700 text-white">
              <el-option label="草稿" value="draft"></el-option>
              <el-option label="未执行" value="not_executed"></el-option>
              <el-option label="通过" value="passed"></el-option>
              <el-option label="失败" value="failed"></el-option>
              <el-option label="阻塞" value="blocked"></el-option>
            </el-select>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-400 mb-1">标签</label>
            <el-input-tag v-model="tags" placeholder="输入标签并按回车添加" class="bg-dark border-gray-700 text-white"></el-input-tag>
          </div>

          <div>
            <label class="block text-sm font-medium text-gray-400 mb-1">描述</label>
            <el-input type="textarea" v-model="description" rows="4" placeholder="输入用例描述" class="bg-dark border-gray-700 text-white"></el-input>
          </div>
        </div>
      </div>

      <!-- 中间测试步骤区域 -->
      <div class="flex-1 flex flex-col bg-dark">
        <!-- 测试步骤工具栏 -->
        <div class="p-4 border-b border-gray-700 flex items-center justify-between bg-dark-card">
          <div class="flex items-center space-x-3">
            <el-button type="text" icon="Plus" class="text-gray-300" @click="addStep">添加步骤</el-button>
            <el-button type="text" icon="Delete" class="text-gray-300" @click="deleteStep">删除步骤</el-button>
            <el-divider direction="vertical" class="bg-gray-700"></el-divider>
            <el-button type="text" icon="ArrowUp" class="text-gray-300" @click="moveStepUp">上移</el-button>
            <el-button type="text" icon="ArrowDown" class="text-gray-300" @click="moveStepDown">下移</el-button>
            <el-divider direction="vertical" class="bg-gray-700"></el-divider>
            <el-button type="text" icon="Copy" class="text-gray-300" @click="copyStep">复制步骤</el-button>
          </div>
          <div class="flex items-center space-x-3">
            <el-button type="text" class="text-primary" @click="togglePreview">预览</el-button>
            <el-button type="text" class="text-primary" @click="importSteps">导入步骤</el-button>
            <el-button type="text" class="text-primary" @click="exportSteps">导出步骤</el-button>
          </div>
        </div>

        <!-- 测试步骤列表 -->
        <div class="flex-1 overflow-y-auto p-4">
          <div class="space-y-4">
            <div v-for="(step, index) in steps" :key="index" class="bg-dark-card rounded-lg p-4 border border-gray-700 hover:border-primary/50 transition-colors">
              <div class="flex items-start">
                <div class="mr-3 mt-1 font-medium text-gray-400">{{ index + 1 }}.</div>
                <div class="flex-1">
                  <div class="mb-2">
                    <label class="block text-sm font-medium text-gray-400 mb-1">操作</label>
                    <el-input v-model="step.action" placeholder="输入操作步骤" class="bg-dark border-gray-700 text-white"></el-input>
                  </div>
                  <div class="mb-2">
                    <label class="block text-sm font-medium text-gray-400 mb-1">目标元素</label>
                    <el-input v-model="step.target" placeholder="输入目标元素" class="bg-dark border-gray-700 text-white"></el-input>
                  </div>
                  <div class="mb-2">
                    <label class="block text-sm font-medium text-gray-400 mb-1">输入数据</label>
                    <el-input v-model="step.data" placeholder="输入数据" class="bg-dark border-gray-700 text-white"></el-input>
                  </div>
                  <div>
                    <label class="block text-sm font-medium text-gray-400 mb-1">预期结果</label>
                    <el-input v-model="step.expected" placeholder="输入预期结果" class="bg-dark border-gray-700 text-white"></el-input>
                  </div>
                </div>
                <div class="ml-3 flex flex-col justify-center">
                  <el-button type="text" icon="Drag" class="text-gray-400 cursor-move"></el-button>
                </div>
              </div>
            </div>
          </div>

          <!-- 空状态 -->
          <div v-if="steps.length === 0" class="flex flex-col items-center justify-center h-64 text-center">
            <div class="w-16 h-16 rounded-full bg-gray-700/50 flex items-center justify-center mb-4">
              <el-icon class="text-gray-400 text-2xl"><List /></el-icon>
            </div>
            <p class="text-gray-400 mb-4">暂无测试步骤</p>
            <el-button type="primary" @click="addStep">添加第一个步骤</el-button>
          </div>
        </div>
      </div>

      <!-- 右侧属性面板 -->
      <div class="w-80 border-l border-gray-700 bg-dark-card p-4 overflow-y-auto">
        <div class="space-y-6">
          <!-- 元素定位 -->
          <div>
            <h3 class="text-lg font-semibold text-white mb-4">元素定位</h3>
            <div class="space-y-3">
              <div>
                <label class="block text-sm font-medium text-gray-400 mb-1">定位方式</label>
                <el-select v-model="locator.type" placeholder="选择定位方式" class="bg-dark border-gray-700 text-white">
                  <el-option label="ID" value="id"></el-option>
                  <el-option label="Name" value="name"></el-option>
                  <el-option label="Class Name" value="class_name"></el-option>
                  <el-option label="CSS Selector" value="css"></el-option>
                  <el-option label="XPath" value="xpath"></el-option>
                  <el-option label="Link Text" value="link_text"></el-option>
                  <el-option label="Partial Link Text" value="partial_link_text"></el-option>
                  <el-option label="Tag Name" value="tag_name"></el-option>
                </el-select>
              </div>
              <div>
                <label class="block text-sm font-medium text-gray-400 mb-1">定位表达式</label>
                <el-input v-model="locator.value" placeholder="输入定位表达式" class="bg-dark border-gray-700 text-white"></el-input>
              </div>
              <el-button type="primary" class="w-full bg-primary hover:bg-primary/90 text-white">验证定位</el-button>
            </div>
          </div>

          <!-- 页面交互 -->
          <div>
            <h3 class="text-lg font-semibold text-white mb-4">页面交互</h3>
            <div class="space-y-3">
              <div>
                <label class="block text-sm font-medium text-gray-400 mb-1">操作类型</label>
                <el-select v-model="interaction.type" placeholder="选择操作类型" class="bg-dark border-gray-700 text-white">
                  <el-option label="点击" value="click"></el-option>
                  <el-option label="输入文本" value="send_keys"></el-option>
                  <el-option label="清除文本" value="clear"></el-option>
                  <el-option label="提交表单" value="submit"></el-option>
                  <el-option label="鼠标悬停" value="hover"></el-option>
                  <el-option label="右键点击" value="context_click"></el-option>
                  <el-option label="双击" value="double_click"></el-option>
                  <el-option label="拖拽" value="drag_and_drop"></el-option>
                </el-select>
              </div>
              <div v-if="interaction.type === 'send_keys'">
                <label class="block text-sm font-medium text-gray-400 mb-1">输入内容</label>
                <el-input v-model="interaction.value" placeholder="输入文本内容" class="bg-dark border-gray-700 text-white"></el-input>
              </div>
              <el-button type="primary" class="w-full bg-primary hover:bg-primary/90 text-white">添加到步骤</el-button>
            </div>
          </div>

          <!-- 断言设置 -->
          <div>
            <h3 class="text-lg font-semibold text-white mb-4">断言设置</h3>
            <div class="space-y-3">
              <div>
                <label class="block text-sm font-medium text-gray-400 mb-1">断言类型</label>
                <el-select v-model="assertion.type" placeholder="选择断言类型" class="bg-dark border-gray-700 text-white">
                  <el-option label="元素存在" value="element_present"></el-option>
                  <el-option label="元素可见" value="element_visible"></el-option>
                  <el-option label="文本包含" value="text_contains"></el-option>
                  <el-option label="属性值" value="attribute_value"></el-option>
                  <el-option label="页面标题" value="page_title"></el-option>
                  <el-option label="页面URL" value="page_url"></el-option>
                  <el-option label="元素数量" value="element_count"></el-option>
                </el-select>
              </div>
              <div v-if="assertion.type === 'text_contains'">
                <label class="block text-sm font-medium text-gray-400 mb-1">预期文本</label>
                <el-input v-model="assertion.value" placeholder="输入预期文本" class="bg-dark border-gray-700 text-white"></el-input>
              </div>
              <el-button type="primary" class="w-full bg-primary hover:bg-primary/90 text-white">添加断言</el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { ArrowLeft, Plus, Delete, ArrowUp, ArrowDown, Copy, Drag, List } from '@element-plus/icons-vue';
import { ElInputTag } from 'element-plus';

// 用例基本信息
const caseName = ref('');
const projectId = ref('');
const suiteId = ref('');
const priority = ref('P2');
const caseType = ref('functional');
const status = ref('draft');
const tags = ref<string[]>([]);
const description = ref('');

// 测试步骤
interface TestStep {
  action: string;
  target: string;
  data: string;
  expected: string;
}

const steps = ref<TestStep[]>([]);
const selectedStepIndex = ref(-1);

// 添加步骤
const addStep = () => {
  steps.value.push({
    action: '',
    target: '',
    data: '',
    expected: ''
  });
  selectedStepIndex.value = steps.value.length - 1;
};

// 删除步骤
const deleteStep = () => {
  if (selectedStepIndex.value !== -1) {
    steps.value.splice(selectedStepIndex.value, 1);
    if (steps.value.length > 0) {
      selectedStepIndex.value = Math.min(selectedStepIndex.value, steps.value.length - 1);
    } else {
      selectedStepIndex.value = -1;
    }
  }
};

// 上移步骤
const moveStepUp = () => {
  if (selectedStepIndex.value > 0) {
    const temp = steps.value[selectedStepIndex.value];
    steps.value[selectedStepIndex.value] = steps.value[selectedStepIndex.value - 1];
    steps.value[selectedStepIndex.value - 1] = temp;
    selectedStepIndex.value--;
  }
};

// 下移步骤
const moveStepDown = () => {
  if (selectedStepIndex.value < steps.value.length - 1 && selectedStepIndex.value !== -1) {
    const temp = steps.value[selectedStepIndex.value];
    steps.value[selectedStepIndex.value] = steps.value[selectedStepIndex.value + 1];
    steps.value[selectedStepIndex.value + 1] = temp;
    selectedStepIndex.value++;
  }
};

// 复制步骤
const copyStep = () => {
  if (selectedStepIndex.value !== -1) {
    const copiedStep = { ...steps.value[selectedStepIndex.value] };
    steps.value.splice(selectedStepIndex.value + 1, 0, copiedStep);
    selectedStepIndex.value++;
  }
};

// 切换预览
const togglePreview = () => {
  // 实现预览功能
};

// 导入步骤
const importSteps = () => {
  // 实现导入功能
};

// 导出步骤
const exportSteps = () => {
  // 实现导出功能
};

// 元素定位
const locator = reactive({
  type: 'css',
  value: ''
});

// 页面交互
const interaction = reactive({
  type: 'click',
  value: ''
});

// 断言设置
const assertion = reactive({
  type: 'element_present',
  value: ''
});

// 项目列表（静态数据）
const projects = [
  { id: '1', name: '电商平台UI自动化' },
  { id: '2', name: '供应链管理系统' },
  { id: '3', name: '财务报表系统API' },
  { id: '4', name: '用户管理系统' }
];

// 测试集列表（静态数据）
const suites = [
  { id: '1', name: '登录模块' },
  { id: '2', name: '商品管理' },
  { id: '3', name: '订单流程' },
  { id: '4', name: '支付功能' }
];
</script>

<style scoped>
.test-case-editor-container {
  min-height: 100vh;
}

/* 自定义滚动条 */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: var(--bg-dark);
}

::-webkit-scrollbar-thumb {
  background: var(--border-gray-700);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: #4b5563;
}
</style>